package com.gla.giliair.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gla.giliair.dao.CompanyPlanemodelDao;
import com.gla.giliair.entity.*;
import com.gla.giliair.service.CompanyPlanemodelService;
import com.gla.giliair.service.CompanyService;
import com.gla.giliair.service.PlanemodelService;
import com.gla.giliair.util.BeanUtil;
import com.gla.giliair.util.MapGenerator;
import com.gla.giliair.web.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/28 14:56
 */
@Service
public class CompanyPlanemodelServiceImpl implements CompanyPlanemodelService {
    @Autowired
    private CompanyPlanemodelDao companyPlanemodelDao;

    @Autowired
    private PlanemodelService planemodelService;

    @Autowired
    private CompanyService companyService;

    @Override
    public CompanyPlanemodel selectCompanyPlanemodelById(Long companyPlanemodelId) {
        if (companyPlanemodelId != null) {
            CompanyPlanemodelExample companyPlanemodelExample = new CompanyPlanemodelExample();
            companyPlanemodelExample.or().andCompanyPlanemodelIdEqualTo(companyPlanemodelId);
            List<CompanyPlanemodel> companyPlanemodelList = companyPlanemodelDao.selectByExample(companyPlanemodelExample);
            if (companyPlanemodelList.isEmpty()) {
                return null;
            }
            return companyPlanemodelList.get(0);
        }
        return null;
    }

    @Override
    public CompanyPlanemodel selectCompanyPlanemodelByCompanyIdAndPlanemodelId(Long companyId, Long planemodelId) {
        if (companyId != null && planemodelId != null) {
            CompanyPlanemodelExample companyPlanemodelExample = new CompanyPlanemodelExample();
            companyPlanemodelExample.or().andCompanyIdEqualTo(companyId).andPlanemodelIdEqualTo(planemodelId);
            List<CompanyPlanemodel> cabinInfoList = companyPlanemodelDao.selectByExample(companyPlanemodelExample);
            if (cabinInfoList.isEmpty()) {
                return null;
            }
            return cabinInfoList.get(0);
        }
        return null;
    }

    @Override
    public List<CompanyPlanemodel> selectPlanemodelIdsByCompanyId(Long companyId) {
        if (companyId != null) {
            CompanyPlanemodelExample companyPlanemodelExample = new CompanyPlanemodelExample();
            //按飞机机型id正序排序
            companyPlanemodelExample.setOrderByClause("planemodel_id asc");
            companyPlanemodelExample.or().andCompanyIdEqualTo(companyId);
            List<CompanyPlanemodel> companyPlanemodelList = companyPlanemodelDao.selectByExample(companyPlanemodelExample);
            return companyPlanemodelList;
        }
        return null;
    }

    @Override
    public Map<String, Object> findPlanemodelsByCompanyId(Long companyId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<CompanyPlanemodel> companyPlanemodelList = selectPlanemodelIdsByCompanyId(companyId);
        PageInfo pageInfo = new PageInfo(companyPlanemodelList);
        if (!companyPlanemodelList.isEmpty()) {
            List<CompanyPlanemodelVO> companyPlanemodelVOList = BeanUtil.copyList(companyPlanemodelList, CompanyPlanemodelVO.class);
            for (CompanyPlanemodelVO companyPlanemodelVO : companyPlanemodelVOList) {
                Planemodel planemodel = planemodelService.selectPlanemodelById(companyPlanemodelVO.getPlanemodelId());
                PlanemodelVO planemodelVO = new PlanemodelVO();
                BeanUtil.copyOne(planemodel, planemodelVO);
                companyPlanemodelVO.setPlanemodel(planemodelVO);
            }
            pageInfo.setList(companyPlanemodelVOList);
            return MapGenerator.getResWithData(200, "查询航空公司所属机型列表成功！", pageInfo);
        } else {
            return MapGenerator.getRes(201, "查询结果为空！");
        }
    }

    @Override
    public Map<String, Object> addCompanyPlanemodel(CompanyPlanemodel companyPlanemodel) {
        Long companyId = companyPlanemodel.getCompanyId();
        Long planemodelId = companyPlanemodel.getPlanemodelId();
        Company companyFromDB = companyService.selectCompanyById(companyId);
        if (companyFromDB == null) {
            return MapGenerator.getRes(502, "航空公司不存在！");
        }
        Planemodel planemodelFromDB = planemodelService.selectPlanemodelById(planemodelId);
        if (planemodelFromDB == null) {
            return MapGenerator.getRes(503, "飞机机型不存在！");
        }
        if (selectCompanyPlanemodelByCompanyIdAndPlanemodelId(companyId, planemodelId) != null) {
            return MapGenerator.getRes(504, "该航空公司旗下已经存在这种飞机机型！");
        }
        //填充创建、更新日期
        Date nowDate = new Date();
        companyPlanemodel.setCreateTime(nowDate);
        companyPlanemodel.setUpdateTime(nowDate);
        if (companyPlanemodelDao.insertSelective(companyPlanemodel) > 0) {
            return MapGenerator.getRes(200, "添加航空公司所属机型成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }

    @Override
    public Map<String, Object> deleteCompanyPlanemodel(Long companyPlanemodelId) {
        CompanyPlanemodel companyPlanemodelFromDB = selectCompanyPlanemodelById(companyPlanemodelId);
        if (companyPlanemodelFromDB == null) {
            return MapGenerator.getRes(501, "航空公司所属机型不存在！");
        }
        if (companyPlanemodelDao.deleteByPrimaryKey(companyPlanemodelId) > 0) {
            return MapGenerator.getRes(200, "删除航空公司所属机型成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }
}
